﻿using System.Linq.Expressions;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace Shard.Contracts;

/// <summary>
/// SqlSugar仓储接口，扩展ABP的IRepository接口
/// </summary>
public interface ISqlSugarRepository<TEntity> : ISqlSugarRepository<TEntity, Guid>
    where TEntity : class, IEntity<Guid>
{
}

/// <summary>
/// SqlSugar仓储接口，扩展ABP的IRepository接口，带主键
/// </summary>
public interface ISqlSugarRepository<TEntity, TKey> : IRepository<TEntity, TKey>
    where TEntity : class, IEntity<TKey>
{
    /// <summary>
    /// 获取完整的SQL语句
    /// </summary>
    string ToSql<TResult>(Expression<Func<TEntity, TResult>> selectExpression);

    /// <summary>
    /// 返回满足条件的第一条记录
    /// </summary>
    Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);

    /// <summary>
    /// 返回满足条件的第一条记录，如果没有则返回默认值
    /// </summary>
    Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 判断是否存在满足条件的记录
    /// </summary>
    Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);

    /// <summary>
    /// 分页查询
    /// </summary>
    Task<(List<TEntity> Items, long TotalCount)> GetPagedListAsync(
        int skipCount,
        int maxResultCount,
        Expression<Func<TEntity, bool>> predicate = null,
        Expression<Func<TEntity, object>> sorting = null,
        bool isAsc = true,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 批量删除
    /// </summary>
    Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);

    /// <summary>
    /// 批量更新
    /// </summary>
    Task<int> UpdateAsync(Expression<Func<TEntity, bool>> predicate,
        Expression<Func<TEntity, TEntity>> updateExpression, CancellationToken cancellationToken = default);

    /// <summary>
    /// 批量插入
    /// </summary>
    Task<int> InsertManyAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
}